package org.col.dspace.tagger.submit.ui;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.col.dspace.tagger.model.TagGenerator;
import org.dspace.app.util.SubmissionInfo;
import org.dspace.app.webui.submit.JSPStep;
import org.dspace.app.webui.util.JSPManager;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Bitstream;
import org.dspace.content.Bundle;
import org.dspace.content.DCValue;
import org.dspace.content.InProgressSubmission;
import org.dspace.content.Item;
import org.dspace.core.Context;
import org.dspace.submit.AbstractProcessingStep;

import com.google.gson.Gson;

/**
 * 
 * @author intern
 * @version May 9 2011
 *
 */

public class JSPTagStep extends JSPStep {

	private static final String PRE_GENERATE_JSP = "/submit/generate-tag.jsp";
	
	private static Logger log = Logger.getLogger(JSPTagStep.class);

	/**
	 * 
	 */
	@Override
	public void doPreProcessing(Context context, HttpServletRequest request,
			HttpServletResponse response, SubmissionInfo subInfo)
			throws ServletException, IOException, SQLException,
			AuthorizeException {

		log.info("doPreProcessing(): launching tag jsp");
		InProgressSubmission progressItem = subInfo.getSubmissionItem();
		Item item = progressItem.getItem();
		Bundle[] bundles = item.getBundles("ORIGINAL");
		Bitstream primaryBitstream =null;
		Bitstream[] bitstreams = bundles[0].getBitstreams();
		if(bitstreams.length ==1)
			primaryBitstream = bitstreams[0];
		else{
			int primaryBsId = bundles[0].getPrimaryBitstreamID();
			primaryBitstream = Bitstream.find(context,primaryBsId);
		}
		if(primaryBitstream == null)
			log.info("no primary bitstream");
		request.setAttribute("primary-bitstream", primaryBitstream);

		//retrieve tags
		DCValue[] tags = item.getMetadata("dc", "description", "tag", Item.ANY);
		log.info(tags.length);
		if (tags.length<=0)
			request.setAttribute("tags", "");
		else
			request.setAttribute("tags",tags[0].value);
		JSPManager.showJSP(request, response, PRE_GENERATE_JSP);

		
	}

	@Override
	public void doPostProcessing(Context context, HttpServletRequest request,
			HttpServletResponse response, SubmissionInfo subInfo, int status)
			throws ServletException, IOException, SQLException,
			AuthorizeException {
		
		switch(status){
		case TagGenerator.STATUS_TAG_GEN_FAILED:
		case TagGenerator.STATUS_TAG_GEN_SUCCESS:
		case TagGenerator.STATUS_TAG_GEN_WRONG_FORMAT:
			Response resp = Response.createResponse(request, status);
			Gson gson =new Gson();
			gson.toJson(resp, response.getWriter());
			response.setContentType( "application/json");
			response.flushBuffer();		
		
		}
	
		
	}

	@Override
	public String getReviewJSP(Context context, HttpServletRequest request,
			HttpServletResponse response, SubmissionInfo subInfo) {
		return null;
	}

}
